﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
namespace syc.Sale
{
    public partial class 辅料采购计划录入0 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            this.Message.Text = "·准备就绪";
            if (!this.IsPostBack)
            {
                if (Request.QueryString["y"] != null)
                {
                    this.NianDu.Text = Request.QueryString["y"].ToString();
                    this.NianDu.Enabled = false;
                    Button_Query_Click(null, null);
                }
                else{
                    this.NianDu.Text = DateTime.Now.Year.ToString();
                    this.SqlDataSource1.SelectCommand = "";
                }
                if (Request.QueryString["m"] != null)
                {
                    this.YueFen.Text = Request.QueryString["m"].ToString();
                    this.YueFen.Enabled = false;
                    Button_Query_Click(null, null);
                }
                else
                {
                    this.YueFen.Text = DateTime.Now.Month.ToString();
                    this.SqlDataSource1.SelectCommand = "";
                }             
                
            }
        }

        protected void Button_Save_Click(object sender, EventArgs e)
        {
            SqlConnection Conn = new SqlConnection();
            Conn.ConnectionString = ConfigurationManager.ConnectionStrings["SCMConnectionString1"].ConnectionString;
            Conn.Open();
            SqlCommand Cmd = new SqlCommand();
            Cmd.Connection = Conn;
            try
            {
                if (this.FuLiao.SelectedItem.Text.Trim() != "其它")
                {
                    if (this.JiHuaLiang.Text == "")
                    {
                        this.Message.Text = "计划采购量不能为空!";
                        return;
                    }
                    else if (this.DanJia.Text == "")
                    {
                        this.Message.Text = "单价不能为空!";
                        return;
                    }
                }
                else
                {
                    if (this.Jine.Text == "")
                    {
                        this.Message.Text = "金额不能为空!";
                        return;
                    }
                }
                if (this.FuLiao.SelectedItem.Text.Trim() != "其它")
                {
                    Cmd.CommandText =
    "if EXISTS(select * from 辅料采购计划0 where 年度=" + this.NianDu.Text + " and 月份=" + this.YueFen.Text + " and 辅料ID=" + this.FuLiao.Text + ")\n"
    + "begin\n"
    + "return\n"
    + "end\n"
    + "else\n"
    + "begin\n"
    + @"insert into 辅料采购计划0 values('" + this.NianDu.Text + "','" + this.YueFen.Text + "','" + FuLiao.Text + "','" + double.Parse(this.JiHuaLiang.Text) + "','" + double.Parse(this.DanJia.Text) + "'"
    + "," + (double.Parse(this.JiHuaLiang.Text) * double.Parse(this.DanJia.Text))
    + ")\n"
    + "end";
                }
                else
                {
                    Cmd.CommandText =
    "if EXISTS(select * from 辅料采购计划0 where 年度=" + this.NianDu.Text + " and 月份=" + this.YueFen.Text + " and 辅料ID=" + this.FuLiao.Text + ")\n"
    + "begin\n"
    + "return\n"
    + "end\n"
    + "else\n"
    + "begin\n"
    + @"insert into 辅料采购计划0 values('" + this.NianDu.Text + "','" + this.YueFen.Text + "','" + FuLiao.Text + "','0','0'"
    + "," + (double.Parse(this.Jine.Text))
    + ")\n"
    + "end";
                }
              //  }          
           
             if (Cmd.ExecuteNonQuery() < 1)
             {
                if (this.NianDu.Enabled)
                {
                    this.Message.Text = "·保存失败，计划已经存在或该辅料已存在清单中";
                    return;
                }
                else
                {
                    this.Message.Text = "·保存失败，计划已经删除或该原料已存在清单中";
                    return;
                }
              }
            else
            {
                this.Message.Text = "·保存成功";
            }
            }
            catch (Exception ex)
            {
                this.Message.Text = "·保存失败!"+ex.Message;
                return;
            }
            Conn.Close();
            Cmd.Dispose();
            Conn.Dispose();
            Button_Query_Click(null,null);
            this.JiHuaLiang.Text = "";
            this.DanJia.Text = "";
        }
        protected void Button_Query_Click(object sender, EventArgs e)
        {
            SqlConnection Conn = new SqlConnection();
            Conn.ConnectionString = ConfigurationManager.ConnectionStrings["SCMConnectionString1"].ConnectionString;
            Conn.Open();
            SqlCommand Cmd = new SqlCommand();
            Cmd.Connection = Conn;
            Cmd.CommandText = "select count(*) from 辅料采购计划0 where 年度=" + this.NianDu.Text + " and 月份=" + this.YueFen.Text;
            if ((int)Cmd.ExecuteScalar() ==0)
            {
                this.SqlDataSource1.SelectCommand = "";
            }
            else
            {
                string sql = "";
                if (this.NianDu.Text != "0")
                {
                    sql = " where 年度=" + this.NianDu.Text + "and 月份=" + this.YueFen.Text;
                }
                sql = "select 年度,月份,辅料ID,品名,规格,单位,计划购进量,辅料采购计划0.单价,金额"
                    + " from 辅料采购计划0 inner join 材料 on 辅料采购计划0.辅料ID=材料.ID " + sql + " order by 年度 desc,月份 asc";
                this.SqlDataSource1.SelectCommand = sql;
            }
            this.GridView1.SelectedIndex = -1;
        }
        protected void Button_Calc_Click(object sender, EventArgs e)
        {

        }
        protected void GridView1_DataBound(object sender, EventArgs e)
        {
            this.TextBox_Pager.Text = (this.GridView1.PageIndex + 1).ToString();
            this.Label_Pages.Text = " 共" + this.GridView1.PageCount.ToString() + "页 ";

            this.Button_Pager_First.CssClass = "pageturn-first-b";
            this.Button_Pager_First.Enabled = true;
            this.Button_Pager_Prev.CssClass = "pageturn-prev-b";
            this.Button_Pager_Prev.Enabled = true;
            this.Button_Pager_Next.CssClass = "pageturn-next-b";
            this.Button_Pager_Next.Enabled = true;
            this.Button_Pager_Last.CssClass = "pageturn-last-b";
            this.Button_Pager_Last.Enabled = true;
            if (this.GridView1.PageIndex == 0)
            {
                this.Button_Pager_Prev.CssClass = "pageturn-prev-g";
                this.Button_Pager_Prev.Enabled = false;
            }
            if (this.GridView1.PageIndex < 2)
            {
                this.Button_Pager_First.CssClass = "pageturn-first-g";
                this.Button_Pager_First.Enabled = false;
            }
            if (this.GridView1.PageIndex == this.GridView1.PageCount - 1 || this.GridView1.PageCount == 0)
            {
                this.Button_Pager_Next.CssClass = "pageturn-next-g";
                this.Button_Pager_Next.Enabled = false;

            }
            if (this.GridView1.PageIndex > this.GridView1.PageCount - 3)
            {
                this.Button_Pager_Last.CssClass = "pageturn-last-g";
                this.Button_Pager_Last.Enabled = false;
            }

        }
        protected void GridView1_PageIndexChanged(object sender, EventArgs e)
        {
            this.GridView1_DataBound(null, null);
        }
        protected void Pager_Click(object sender, EventArgs e)
        {
            if (sender.GetType().ToString() == "System.Web.UI.WebControls.Button")
            {
                switch (((Button)sender).CommandName)
                {
                    case "--":
                        this.GridView1.PageIndex = 0;
                        break;
                    case "-":
                        if (this.GridView1.PageIndex > 0) this.GridView1.PageIndex--;
                        break;
                    case "+":
                        if (this.GridView1.PageIndex < this.GridView1.PageCount - 1) this.GridView1.PageIndex++;
                        break;
                    case "++":
                        this.GridView1.PageIndex = this.GridView1.PageCount - 1;
                        break;
                }
            }
            else
            {
                int p;
                if (int.TryParse(TextBox_Pager.Text, out p))
                {
                    if (p >= 1 && p <= this.GridView1.PageCount)
                    {
                        this.GridView1.PageIndex = p + 1;
                    }
                }
                else
                {
                    TextBox_Pager.Text = (this.GridView1.PageIndex + 1).ToString();
                }
            }

        }

        protected void Button_Add_Click(object sender, EventArgs e)
        {
            this.Button_Add.Visible = false;
            this.Button_Save.Visible = true;
            this.Panel3.Visible = true;
        }

        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GridView1.EditIndex = e.NewEditIndex;

        }

        protected void FuLiao_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.FuLiao.SelectedItem.Text.Trim()== "其它")
            {
                this.JiHuaLiang.Visible = false;
                this.lblLiang.Visible = false;
                this.DanJia.Visible = false;
                this.lblJia.Visible = false;
                this.lblJine.Visible = true;
                this.Jine.Visible = true;             
                
            }
            else
            {
                this.JiHuaLiang.Visible = true;
                this.lblLiang.Visible = true;
                this.DanJia.Visible = true;
                this.lblJia.Visible = true;
                this.lblJine.Visible = false;
                this.Jine.Visible = false;              
            }
        }

        
    }
}
